<?php

namespace app\shop\controller;

use think\Request;
use think\Exception;
use app\shop\model\Invoice as InvoiceModel;

class Invoice extends \ShopController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $invoiceModel = new InvoiceModel();

        $userId = \request()->header('userId');

        try {
            $data = $invoiceModel->where([
                'user_id' => $userId,
            ])->find();

            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $invoiceModel = new InvoiceModel();

        try {

            $this->successTerminate();
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $invoiceModel = new InvoiceModel();

        $data = $request->post();

        $data['user_id'] = $request->header('userId');

        /* @var $validate \ShopValidate */
        $validate = \think\Loader::validate('Invoice');
        if ( !$validate->scene('')->check($data) ) {
            return $validate->getError();
        }

        try {

            $result = $invoiceModel->data($data)->allowField(true)->save();

            if (! $result) {
                $this->serverErrorTerminate('资源新增失败');
            }

            $this->successTerminate($invoiceModel);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        $invoiceModel = new InvoiceModel();

        try {
            $data = $invoiceModel->get($id);
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $invoiceModel = new InvoiceModel();

        try {

            $this->successTerminate();
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request)
    {
        $invoiceModel = new InvoiceModel();
        $data = $request->post();

        $pk = $invoiceModel->getPk();
        $id = $data[$pk];
        if ( isset($data[$pk]) ) unset($data[$pk]);

        try {
            $affectedRowCount = $invoiceModel->allowField(true)->save($data, [
                $pk => $id,
            ]);
//            if (! $affectedRowCount) $this->serverErrorTerminate('更新资源失败');
            $this->successTerminate($invoiceModel);
        } catch (Exception $e) {
//            $this->serverErrorTerminate($invoiceModel->getLastSql());
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $invoiceModel = new InvoiceModel();

        try {

            $this->successTerminate();
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }
}
